package com.xing.hospital.common.helper;

import com.alibaba.fastjson.JSONObject;
import com.xing.common.utils.HttpUtil;
import com.xing.hospital.common.utils.MD5;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author 邢福豪
 * @date 2022/8/10 11:18
 */

@Slf4j
public class HttpRequestHelper {

    public static void main(String[] args) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("d", "4");
        paramMap.put("c", "3");
        paramMap.put("b", "2");
        paramMap.put("a", "1");
        paramMap.put("timestamp", getTimestamp());
        log.info(getSign(paramMap, "11111111"));
    }


    /**
     * 请求数据获取签名
     *
     * @param paramMap
     * @param signKey
     * @return
     */
    public static String getSign(Map<String, Object> paramMap, String signKey) {
        if (paramMap.containsKey("sign")) {
            paramMap.remove("sign");
        }
        TreeMap<String, Object> sorted = new TreeMap<>(paramMap);
        StringBuilder str = new StringBuilder();
        for (Map.Entry<String, Object> param : sorted.entrySet()) {
            str.append(param.getValue()).append("|");
        }
        str.append(signKey);
        log.info("xfh======>加密前：" + str.toString());
        String md5Str = MD5.encrypt(str.toString());
        log.info("xfh======>加密后：" + md5Str);
        return md5Str;
    }

    /**
     * 获取时间戳
     *
     * @return
     */
    public static long getTimestamp() {
        log.info("xfh======>时间戳：" + new Date().getTime());
        return new Date().getTime();
    }

    /**
     * 签名校验
     *
     * @param paramMap
     * @param signKey
     * @return
     */
    public static boolean isSignEquals(Map<String, Object> paramMap, String signKey) {
        String sign = (String) paramMap.get("sign");
        String md5Str = getSign(paramMap, signKey);
        if (!sign.equals(md5Str)) {
            return false;
        }
        return true;
    }


    /**
     * 封装同步请求
     *
     * @param paramMap
     * @param url
     * @return
     */
    public static JSONObject sendRequest(Map<String, Object> paramMap, String url) {
        String result = "";

        try {
            //封装post参数
            StringBuilder postdata = new StringBuilder();
            for (Map.Entry<String, Object> param : paramMap.entrySet()) {
                postdata.append(param.getKey())
                        .append("=")
                        .append(param.getValue()).append("&");
            }
            log.info(String.format("xfh--->发送请求：post data 发送的数据： %1s", postdata));
            byte[] reqData = postdata.toString().getBytes("utf-8");
            byte[] respdata = HttpUtil.doPost(url, reqData);
            result = new String(respdata);
            log.info(String.format("xfh--->应答结果：result data 应答的结果数据： %1s", result));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(result);


    }


}
